import {createRouter, createWebHashHistory, RouteRecordRaw} from 'vue-router';
import {afterEach, beforeEach} from './hook';

const routes: Array<RouteRecordRaw> = [{path: '/', redirect: '/home'}];

const pages = import.meta.glob('../pages/**/Page.vue');
const configs = import.meta.glob('../pages/**/config.ts', {eager: true});

Object.entries(configs).forEach(([key, val]: any) => {
  const pagesPath = key.replace('/config.ts', '/Page.vue');
  routes.push({
    path: '/' + key.replace(/\/config\.ts|\.\.\/pages\//g, ''),
    meta: {title: val.default.title},
    component: pages[pagesPath]
  });
});

const router = createRouter({
  history: createWebHashHistory(),
  routes
});

beforeEach(router);
afterEach(router);

export default router;
